package models;


import javax.persistence.*;

import play.db.jpa.Model;

@Entity
public class Eolienne extends Model {
	
	static final int N = 8760;
	
	private Double _v1;
	private Double _v2;
	private Double _v3;
	private Double _surfaceBalayer;

	private Vent ecretageEolienne(Vent vents) {
		Vent ventsCaptes = new Vent();
		Courbe courbeVents = vents.getCourbeVents();
		Courbe courbeVentsCaptes = ventsCaptes.getCourbeVents();
	    for (int i = 0; i < N; i++) {
	    	if (courbeVents.getPoint(i) < _v1){
	    		courbeVentsCaptes.setPoint(i, 0);
	    	}
	    	else if (courbeVents.getPoint(i) > _v2) {
	    		courbeVentsCaptes.setPoint(i, _v2);
	    	}
	    	else if (courbeVents.getPoint(i) > _v3) {
	    		courbeVentsCaptes.setPoint(i, 0);
	    	}
	    	else {
	    		courbeVentsCaptes.setPoint(i, courbeVents.getPoint(i));
	    	}
	    }
	    ventsCaptes.setCourbeVents(courbeVentsCaptes);
	    return ventsCaptes;
	}
	
	private Courbe conversionMsEnKw(Vent vents, Temperature temperatures) {
		Double a = -0.004531;
		Double b = 1.2986;
		Double coefDePuissance = 0.59;
		Courbe courbeVents = vents.getCourbeVents();
		
		// ventsAuCube = vents.^3;
		for (int i = 0; i < courbeVents.getSize(); i++) {
			courbeVents.setPoint(i, Math.pow(courbeVents.getPoint(i), 3));
		}

		// masseVolumique = ones(n,1)*a.*temperatures + b
		Courbe masseVolumique = new Courbe(N, a);
		masseVolumique = masseVolumique.multiple(temperatures.getCourbeTemperatures()).add(b);
		
		// courbeProduction = coefDePuissance*(1/2)*surfaceBalayer;
		Courbe courbeProduction = new Courbe(N, coefDePuissance*0.5*_surfaceBalayer);
		
		// courbeProduction =courbeProduction.*masseVolumique;
		courbeProduction = courbeProduction.multiple(masseVolumique);
		
		// courbeProduction = courbeProduction.*ventsAuCube;
		courbeProduction = courbeProduction.multiple(courbeVents);
		
		return courbeProduction;
	}
	
	public Courbe calculProductibleEolienne(Vent vents, Temperature temperatures) {
		 Vent ventsCaptes = ecretageEolienne(vents);
		 return conversionMsEnKw(ventsCaptes, temperatures);
	}
}